package com.easyidea.its.facade.department.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.domain.service.department.DepartmentService;
import com.easyidea.its.domain.service.department.PassPortService;
import com.easyidea.its.facade.department.PassPortMgr;

public class PassPortMgrImpl implements PassPortMgr {

	private PassPortService passPortService;
	
	private DepartmentService departmentService;
	
	public List<PassPort> getPassPorts(PassPort passport, int startAtIndex, int pageSize) {
		// TODO Auto-generated method stub
		return passPortService.getPassPorts(passport, startAtIndex, pageSize);
	}

	public int getPassPortsCount(PassPort passport) {
		// TODO Auto-generated method stub
		return passPortService.getPassPortsCount(passport);
	}

	public boolean hasSamePassPortName(String passPortName, Integer id) {
		// TODO Auto-generated method stub
		return passPortService.hasSamePassPortName(passPortName, id);
	}

	public void setPassPortService(PassPortService passPortService) {
		this.passPortService = passPortService;
	}
	public boolean saveOrUpdate(PassPort passPort) {
		if(passPortService.hasSamePassPortName(passPort.getPassPortName(), passPort.getId())) {
			 return false; 
		} else {
			if (passPort.getId() == -1) {
				this.passPortService.save(passPort);
			} else {
				PassPort old = this.getPassPortById(passPort.getId());
//				old.setPassPortName(passPort.getPassPortName());
//				old.setDepartment(passPort.getDepartment());
//				old.setLatitudeCoor(passPort.getLatitudeCoor());
//				old.setLongitudeCoor(passPort.getLongitudeCoor());
				try {
					BeanUtils.copyProperties(old, passPort);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.passPortService.update(old);
			}			
		}

		return true; 
	 }
	public PassPort getPassPortById(Integer id) {
		return this.passPortService.getBeanById(id);
	}
	public void removePassPortById(Integer id) {
		
			this.passPortService.removePassPortById(id);			
		
	}
	public Collection<Department> getAllDepartments() {
		return departmentService.getAllDepartments();
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
}
